{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-10T04:30:23.337887Z",
     "start_time": "2025-05-10T04:30:16.749727Z"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "driver = webdriver.Edge()\n",
    "driver.get('https://book.douban.com/review/best/')\n",
    "\n",
    "# 定位输入框\n",
    "driver.find_element(By.ID,'inp-query').send_keys(\"斗破苍穹\")\n",
    "# 定位搜索按钮\n",
    "driver.find_element(By.XPATH,'//*[@id=\"db-nav-book\"]/div[1]/div/div[2]/form/fieldset/div[2]/input').click()\n",
    "\n",
    "time.sleep(2)\n",
    "\n",
    "# 切换窗口\n",
    "driver.switch_to.window(driver.window_handles[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "divs = driver.find_elements(By.CLASS_NAME,'sc-bZQynM.KbQMe.sc-bxivhb.eJWSlY')\n",
    "for div in divs:\n",
    "    name = div.find_element(By.CLASS_NAME,\"title\").text # 书名_斗破苍穹\n",
    "    score = div.find_element(By.CLASS_NAME,'rating_nums').text # 评分_6.4\n",
    "    qty = div.find_element(By.CLASS_NAME,'pl').text  # 评价人数_(766人评价)\n",
    "    all_data = div.find_element(By.CLASS_NAME,'meta.abstract').text.split(\" / \") # 天蚕土豆 / 湖北少年儿童出版社 / 2010-7 / 21.00元\n",
    "    author = all_data[0] # 作者——'天蚕土豆' \n",
    "    for it in all_data:\n",
    "        if \"出版社\" in it:\n",
    "             publisher = it # 出版社——'湖北少年儿童出版社'\n",
    "    dataTime = all_data[-2] # 时间——'2012-6'\n",
    "    money = all_data[-1] # 价格——'28.00元'\n",
    "    print(f\"{name} {author} {publisher} {qty} {dataTime} {money} {score}\")\n",
    "    \n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-05-10T04:31:06.322253Z",
     "start_time": "2025-05-10T04:31:06.307684Z"
    }
   },
   "id": "18879ee225b000d1"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "time.sleep(2)\n",
    "\n",
    "driver.find_element(By.XPATH,'//*[@id=\"root\"]/div/div[2]/div[1]/div[1]/div[18]/a[3]').click()\n",
    "time.sleep(2)\n",
    "driver.switch_to.window(driver.window_handles[-1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-05-10T04:31:14.854633Z",
     "start_time": "2025-05-10T04:31:10.125126Z"
    }
   },
   "id": "ea33a9a6b06a49d8"
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "divs = driver.find_elements(By.CLASS_NAME,'sc-bZQynM.iNkrne.sc-bxivhb.eJWSlY')\n",
    "# for div in divs:\n",
    "#     name = div.find_element(By.CLASS_NAME,\"title\").text # 书名_斗破苍穹\n",
    "#     score = div.find_element(By.CLASS_NAME,'rating_nums').text # 评分_6.4\n",
    "#     qty = div.find_element(By.CLASS_NAME,'pl').text  # 评价人数_(766人评价)\n",
    "#     all_data = div.find_element(By.CLASS_NAME,'meta.abstract').text.split(\" / \") # 天蚕土豆 / 湖北少年儿童出版社 / 2010-7 / 21.00元\n",
    "#     author = all_data[0] # 作者——'天蚕土豆' \n",
    "#     for it in all_data:\n",
    "#         if \"出版社\" in it:\n",
    "#              publisher = it # 出版社——'湖北少年儿童出版社'\n",
    "#     dataTime = all_data[-2] # 时间——'2012-6'\n",
    "#     money = all_data[-1] # 价格——'28.00元'\n",
    "#     print(f\"{name} {author} {publisher} {qty} {dataTime} {money} {score}\")\n",
    "print(len(divs))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-19T04:01:21.799642200Z",
     "start_time": "2024-06-19T04:01:21.776421100Z"
    }
   },
   "id": "637010ddbe214fc6"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
